using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using TallComponents.PDF.Rasterizer.Configuration;
using TallComponents.Web.Pdf.Services;
using TallComponents.Web.Storage;

namespace TallComponents.Web.Pdf
{
   internal sealed class PrintPreviewGenerator : PreviewGenerator
   {
      /// <summary>
      /// Generates the preview.
      /// </summary>
      /// <param name="context">The context.</param>
      /// <param name="request">The request.</param>
      /// <param name="storage">The storage.</param>
      /// <param name="logger">The logger.</param>
      /// <param name="settings">The settings.</param>
      public override byte[] GeneratePreview(HttpContext context, PreviewRequesParameters request, IPdfStorage storage, ILog logger, RenderSettings settings)
      {
         byte[] data = null;

         Page page = storage.GetPage(request.Key, request.FirstPageIndex);

         int scale = 3;

         int width = (int)page.Width;
         int height = (int)page.Height;

         int rotation = request.Rotation[0];

         switch (rotation)
         {
            case 0:
            case 180:
               break;
            case 90:
            case 270:
               width = (int)page.Height;
               height = (int)page.Width;
               break;
         }

         width *= scale;
         height *= scale;

         using (var bitmap = new Bitmap(width, height))
         {
            using (var graphics = Graphics.FromImage(bitmap))
            {
               if (request.HighQuality)
               {
                  settings.ColorSettings.TransformationMode = ColorTransformationMode.HighSpeed;
                  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                  graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
               }
               else
               {
                  settings.ColorSettings.TransformationMode = ColorTransformationMode.HighSpeed;
               }

               graphics.ScaleTransform(scale, scale);
               GeneratePreview(graphics, context, request, storage, logger, settings);


               using (var buffer = new System.IO.MemoryStream())
               {
                  bitmap.Save(buffer, System.Drawing.Imaging.ImageFormat.Png);
                  data = buffer.ToArray();
               }
            }
         }

         return data;
      }

      /// <summary>
      /// Generates the preview.
      /// </summary>
      /// <param name="graphics">The graphics.</param>
      /// <param name="context">The context.</param>
      /// <param name="request">The request.</param>
      /// <param name="storage">The storage.</param>
      /// <param name="logger">The logger.</param>
      /// <param name="settings">The settings.</param>
      protected override void GeneratePreview(Graphics graphics, HttpContext context, PreviewRequesParameters request, IPdfStorage storage, ILog logger, RenderSettings settings)
      {
         Page page = storage.GetPage(request.Key, request.FirstPageIndex);

         // Ensure page is valid to prevent exceptions
         if (page == null)
         {
            graphics.Clear(Color.FromArgb(0, 0, 0, 0));
            return;
         }

         if (!context.Response.IsClientConnected)
            return;

         page = storage.GetPage(request.Key, request.FirstPageIndex);

         graphics.SmoothingMode = SmoothingMode.None;

         ApplyRotationTransformation(graphics, page, request.Rotation[0]);
         DrawPage(graphics, settings, page, logger);

      }
   }
}